 /******************************************************************************

 Copyright (c) 2012-2016, Texas Instruments Incorporated
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 *  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

 *  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

 *  Neither the name of Texas Instruments Incorporated nor the names of
    its contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 ******************************************************************************
 Release Name: ble_sdk_2_02_00_31
 Release Date: 2016-06-16 18:57:29
 *****************************************************************************/

/*********************************************************************
 * INCLUDES
 */

#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/drivers/UART.h>

/* Example/Board Header files */
#include "Board.h"

#ifdef DEVICE_FAMILY
    #undef DEVICE_FAMILY_PATH
    #define DEVICE_FAMILY_PATH(x) <ti/devices/DEVICE_FAMILY/x>
    #include DEVICE_FAMILY_PATH(driverlib/flash.h)
#else
    #error "You must define DEVICE_FAMILY at the project level as one of cc26x0, cc26x0r2, cc13x0, etc."
#endif

#include <string.h>

#include "oad/wsn_oad_server.h"
#include "oad/oad.h"

#include "oad/oad_target.h"
#include "bim/ext_flash_layout.h"

#include "wsnprotocol/WsnProtocol.h"

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

/*********************************************************************
 * LOCAL VARIABLES
 */
static uint32_t imageAddress;
static uint16_t imagePage;

/*********************************************************************
 * LOCAL FUNCTIONS
 */
static void getNextBlock(uint16_t blkNum, uint8_t* oadBlockBuff);

int32_t WOS_startFwUpdate(uint32_t nodeAddr)
{
    uint8_t imgInfoData[16];
    img_hdr_t remoteImgHdr;
    uint16_t oadBlkTot;
    OADTarget_getRemoteImageHeader(&remoteImgHdr);

    if(remoteImgHdr.len != 0x0000)
    {
        OADTarget_open();

        // Determine where image will be read from.
        imageAddress = OADTarget_imageAddress((uint8_t*)&remoteImgHdr);
        imagePage = imageAddress / FlashSectorSizeGet();

        OADTarget_readFlash(imagePage, 0, imgInfoData, 16);

        /* send the RADIO_PACKET_TYPE_OAD_IMG_IDENTIFY_REQ */
        WSNProtocol_sendImgIdentifyReq(nodeAddr, imgInfoData);

        // Calculate block total of the new image.
        oadBlkTot = remoteImgHdr.len /
                    (OAD_BLOCK_SIZE / 4);

        return (int32_t) oadBlkTot;
    }
    else
    {
        return -1;
    }
}

int32_t WOS_nextBlock(uint32_t nodeAddr, uint16_t blockNum)
{
    //allocate buffer for block + block number
    uint8_t blockBuff[OAD_BLOCK_SIZE + 2] = {0};
    getNextBlock(blockNum, blockBuff);

    WSNProtocol_sendOadImgBlockRsp(nodeAddr, blockBuff);

    return 0;
}

void WOS_endFwUpdate(void)
{
    OADTarget_close();
}

static void getNextBlock(uint16_t blkNum, uint8_t* oadBlockBuff)
{
    oadBlockBuff[0] = blkNum & 0xFF;
    oadBlockBuff[1] = (blkNum & 0xFF00) >> 8;

    // Write a 16 byte block to Flash.
    OADTarget_readFlash(imagePage, (blkNum * OAD_BLOCK_SIZE), oadBlockBuff+2,
                         OAD_BLOCK_SIZE);
}

/*********************************************************************
*********************************************************************/
